using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Rolodex.Model;
using Rolodex.Views;

namespace Rolodex.ApplicationControllers
{
    public partial class Controller
    {
        #region Methods
        public void CardAdministrationView_Show(
            CardAdministrationMode theMode,
            Card theSelectedCard)
        {
            CardView theView =
                new CardView();

            theView.textboxTitle.Leave +=
                new EventHandler(CardAdministrationView_textboxTitle_Leave);
            theView.buttonOK.Click +=
                new EventHandler(CardAdministrationView_buttonOK_Click);
            theView.buttonCancel.Click +=
                new EventHandler(CardAdministrationView_buttonCancel_Click);

            theView.Card.DataSource =
                theSelectedCard;
            theView.Mode =
                theMode;

            theView.Show(this._MainView);
        }

        #region Event Handlers
        private void CardAdministrationView_textboxTitle_Leave(object sender, EventArgs e)
        {
            TextBox theTitleTextBox =
                sender as TextBox;
            CardView theParentView =
                theTitleTextBox.FindForm() as CardView;

            if (String.IsNullOrEmpty(theParentView.textboxLetter.Text) &&
                !String.IsNullOrEmpty(theParentView.textboxTitle.Text))
            {
                Card theCurrentCard =
                    theParentView.Card.Current as Card;

                theCurrentCard.Title =
                    theParentView.textboxTitle.Text;
                theCurrentCard.Letter =
                    theParentView.textboxTitle.Text.Substring(0, 1).ToCharArray()[0];

                theParentView.Card.DataSource =
                    theCurrentCard;

                theParentView.Card.ResetBindings(false);
            }
        }
        
        private void CardAdministrationView_buttonCancel_Click(object sender, EventArgs e)
        {
            Button theOKButton =
                sender as Button;
            CardView theParentView =
                theOKButton.FindForm() as CardView;

            theParentView.Close();
        }

        private void CardAdministrationView_buttonOK_Click(object sender, EventArgs e)
        {
            Button theOKButton =
                sender as Button;
            CardView theParentView =
                theOKButton.FindForm() as CardView;
            Card theCurrentCard =
                theParentView.Card.Current as Card;

            if (theParentView.IsFormValid())
            {
                switch (theParentView.Mode)
                {
                    case CardAdministrationMode.Edit:
                        break;
                    default:
                        this.CardCollection.Add(theCurrentCard);
                        break;
                }

                this.SortCardCollection();

                this.WriteCardCollection();

                this.ResetNumbering();

                this._MainView.Cards.DataSource =
                    this.CardCollection.CardList;

                this._MainView.Cards.ResetBindings(false);

                theParentView.Close();
            }
        }
        #endregion
        #endregion
    }
}
